package com.fuhe.chen.vendingmachine.controller.admin;

import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.common.Utils;
import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.dto.ApiResponse;
import com.fuhe.chen.vendingmachine.dto.cond.AdvertisementCond;
import com.fuhe.chen.vendingmachine.pojo.Advertisement;
import com.fuhe.chen.vendingmachine.service.IAdvertisementService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;

/**
 * 广告管理
 */
@Api(tags = "广告管理")
@Controller
@RequestMapping("/admin/advertisement")
public class AdvertisementController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdvertisementController.class);

    @Autowired
    IAdvertisementService advertisementService;

    @GetMapping({"/query"})
    @ApiOperation("广告列表查询")
    @ResponseBody
    public ApiResponse<PageInfo<Advertisement>> queryAdvertisement(
                                     @RequestParam(name = "page", required = false) Integer page,
                                     @RequestParam(name = "status", required = false) Integer status,
                                     @RequestParam(name = "advertisement_name", required = false) String advertisement_name) {
        AdvertisementCond advertisementCond = new AdvertisementCond();
        advertisementCond.setStatus(status);
        advertisementCond.setAdvertisement_name(advertisement_name);

        PageInfo<Advertisement> advertisements = advertisementService.findByCondition(advertisementCond, page == null ? 1 : page, 10);

        ApiResponse<PageInfo<Advertisement>> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(advertisements);
        return response;
    }

    @PostMapping("/addAdvertisement")
    @ApiOperation("新增广告信息")
    @ResponseBody
    @AddLog(operation = "新增广告信息")
    public ApiResponse<String> addAdvertisement(String advertisement_name, MultipartFile picture) {
        ApiResponse<String> response = new ApiResponse<>();
        String imgPath = "#";

        if (StringUtils.isEmpty(advertisement_name)) {
            response.setSuccess(false);
            response.setCode(MessageConstant.Error.INVALID_PARAM_CODE);
            response.setMessage(MessageConstant.Common.INVALID_PARAM + ":广告名称不能为空");
            return response;
        }

        Advertisement advertisement = new Advertisement();
        advertisement.setAdvertisement_name(advertisement_name);
        advertisement.setCreate_time(String.valueOf(System.currentTimeMillis()));
        advertisement.setStatus(1);
        //将图片文件写入服务器
        advertisement.setPic_url(Utils.upload(picture, imgPath));

        advertisementService.addAdvertisement(advertisement);

        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.UPDATE_SUCCESS_MESSAGE);
        return response;
    }

    @PostMapping("/updateAdvertisement")
    @ApiOperation("广告信息修改")
    @ResponseBody
    @AddLog(operation = "广告信息修改")
    public ApiResponse<String> updateAdvertisement(@RequestParam(name = "advertisementId", required = true) Integer advertisementId,
                                                   @RequestParam(name = "advertisement_name", required = false) String advertisement_name,
                                                   @RequestParam(name = "picture", required = false) MultipartFile picture

    ) {
        ApiResponse<String> response = new ApiResponse<>();
        String imgPath = "#";

        Advertisement advertisement = advertisementService.findOne(advertisementId);
        //广告id参数有效性检测
        if (advertisement == null) {
            response.setSuccess(false);
            response.setCode(MessageConstant.Error.INVALID_PARAM_CODE);
            response.setMessage(MessageConstant.Common.INVALID_PARAM + ":advertisementId");
            return response;
        }

        if(!StringUtils.isEmpty(advertisement_name)){
            advertisement.setAdvertisement_name(advertisement_name);
        }
        //将图片文件写入服务器
        advertisement.setPic_url(Utils.upload(picture, imgPath));
        advertisementService.updateAdvertisement(advertisement);

        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.UPDATE_SUCCESS_MESSAGE);
        return response;
    }

    @PostMapping("/changeShelf")
    @ApiOperation("广告上架/下架 action参数参考值:start--上架,stop--下架")
    @ResponseBody
    @AddLog(operation = "广告上架/下架")
    public ApiResponse<String> changeShelf(@RequestParam(name = "advertisementId", required = true) Integer advertisementId,
                                           @RequestParam(name = "action", required = true) String action) {
        ApiResponse<String> response = new ApiResponse<>();

        Advertisement advertisement = advertisementService.findOne(advertisementId);

        //广告id参数有效性检测
        if (advertisement == null) {
            response.setSuccess(false);
            response.setCode(MessageConstant.Error.INVALID_PARAM_CODE);
            response.setMessage(MessageConstant.Common.INVALID_PARAM + ":advertisementId");
            return response;
        }

        //动作参数有效性检测
        if (action.equals("start")) {
            advertisement.setBegin_time(String.valueOf(System.currentTimeMillis()));
            advertisement.setStatus(0);
        }else if (action.equals("stop")) {
            Long usageTime = advertisement.getUsage_time() == null ? 0L : Long.valueOf(advertisement.getUsage_time());
            String usage_time = String.valueOf(System.currentTimeMillis() - Long.valueOf(advertisement.getBegin_time()) + usageTime);
            advertisement.setUsage_time(usage_time);
            advertisement.setStatus(1);
        } else {
            response.setSuccess(false);
            response.setCode(MessageConstant.Error.INVALID_PARAM_CODE);
            response.setMessage(MessageConstant.Common.INVALID_PARAM + ":action");
            return response;
        }
        advertisementService.updateAdvertisement(advertisement);

        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.UPDATE_SUCCESS_MESSAGE);
        return response;
    }

    @PostMapping("/deleteAdvertisement")
    @ApiOperation("广告删除")
    @ResponseBody
    @AddLog(operation = "广告删除")
    public ApiResponse<String> deleteAdvertisement(@RequestParam(name = "advertisementId", required = true) Integer advertisementId) {
        ApiResponse<String> response = new ApiResponse<>();
        advertisementService.deleteAdvertisement(advertisementId);

        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.DO_SUCCESS_MESSAGE);
        return response;
    }
}
